﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Agen.ServiceAgencyWPF;
using Agen.Models;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace Agen.Views
{
    public partial class scorecode : Page
    {
        private ScoreCodeVO vo;

        public scorecode()
        {
            
            InitializeComponent();
            vo = new ScoreCodeVO();
            DataContext = vo;
            ApplicationManager.client.GetCodesByCustomerIDCompleted += new EventHandler<GetCodesByCustomerIDCompletedEventArgs>(client_GetCodesByCustomerIDCompleted);
            ApplicationManager.client.DeleteCodeToCustomerCompleted += new EventHandler<DeleteCodeToCustomerCompletedEventArgs>(client_DeleteCodeToCustomerCompleted);
            ApplicationManager.client.AddCodeToUserCompleted += new EventHandler<AddCodeToUserCompletedEventArgs>(client_AddCodeToUserCompleted);
        }

        void client_GetCodesByCustomerIDCompleted(object sender, GetCodesByCustomerIDCompletedEventArgs e)
        {
            vo.ListCode = e.Result;
        }

        void client_AddCodeToUserCompleted(object sender, AddCodeToUserCompletedEventArgs e)
        {
            if (e.Result)
            {

                ApplicationManager.client.GetCodesByCustomerIDAsync(ApplicationManager.CURRENT_CUSTOMER.IdCustomer);
                
            }
            else {
                MessageBox.Show("Erreur lros de l'ajout !");
            }
        }

        void client_DeleteCodeToCustomerCompleted(object sender, DeleteCodeToCustomerCompletedEventArgs e)
        {
            if(e.Result != 0){
                foreach(CodeDC code in vo.ListCode){
                    if(code.IdCode == e.Result){
                        vo.ListCode.Remove(code);
                        break;
                    }
                }
            }
        }
        


        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ApplicationManager.client.GetCodesByCustomerIDAsync(ApplicationManager.CURRENT_CUSTOMER.IdCustomer);
        }


        private void addCode_btn_Click(object sender, RoutedEventArgs e)
        {
            ApplicationManager.client.AddCodeToUserAsync(vo.Code);
 
        }

        private void deleteCode_btn_Click(object sender, RoutedEventArgs e)
        {
            CodeDC code = (sender as Button).Tag as CodeDC;
           ApplicationManager.client.DeleteCodeToCustomerAsync(code.IdCode);
        }

        private void cancel_button_Click(object sender, RoutedEventArgs e)
        {
            ApplicationManager.FrameAction(this, ApplicationManager.PREVIOUS_HANDLE, new Object());
        }


    }

    public class ScoreCodeVO : INotifyPropertyChanged
    {

        private ObservableCollection<CodeDC> _listCode;
        private CodeDC _code;

        public ScoreCodeVO() {
            this.Code = new CodeDC();
            this.Code.Date = DateTime.Now;
            this.Code.IdCustomer = ApplicationManager.CURRENT_CUSTOMER.IdCustomer;

            this.ListCode = new ObservableCollection<CodeDC>();

            
        }

        public void refreshCode(){
            this.Code = new CodeDC();
            this.Code.Date = DateTime.Now;
            this.Code.IdCustomer = ApplicationManager.CURRENT_CUSTOMER.IdCustomer;
        }

        public CodeDC Code
        {
            get
            {
                return this._code;
            }
            set
            {
                this._code = value;
                OnPropertyChanged("Code");
            }
        }

        public ObservableCollection<CodeDC> ListCode
        {
            get
            {
                return this._listCode;
            }
            set
            {
                this._listCode = value;
                OnPropertyChanged("ListCode");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    
    }
}
